<?php
namespace app\model;

use app\model\BaseModel;

/**
 * 网站
 */

class EaWxSite extends BaseModel
{
    protected $table = 'ea_wx_site';

    /**
     * [siteGroupName 获取网站分类]
     * @since  2025-05-29
     * @author Dai哥
     * @return [type]     [description]
     */
    public static function siteGroupName()
    {
        return self::field('type2 as type')->where('status', 1)->group('type2')->cache(true)->select()->toArray();
    }

    /**
     * [seFilterBar 过滤条件]
     * @since  2025-06-04
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function seFilterBar($data)
    {
        $model = self::where('status', 1)->order('id DESC');

        if (isset($data['type']) && $data['type'] !== '') {

            $model = $model->where('type2', $data['type']);
        }

        if (isset($data['son']) && $data['son'] !== '') {

            $model = $model->where('type3', $data['son']);
        }

        if (isset($data['id']) && $data['id'] !== '') {

            $model = $model->where('id', $data['id']);
        }

        if (isset($data['title']) && $data['title'] !== '') {

            $model = $model->where('title', $data['title']);
        }

        return $model;
    }

    /**
     * [getRowsTotal 获取数量]
     * @since  2025-06-04
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getRowsTotal($data)
    {
        return self::seFilterBar($data)->count();
    }

    /**
     * [getPlatformList 获取列表]
     * @since  2025-06-04
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getPlatformList($data)
    {
        # 获取总记录数
        $total = self::getRowsTotal($data);

        # 获取记录列表
        $rows = self::seFilterBar($data)
            ->field('*')
            ->page($data['page'], $data['pageSize'])
            ->cache(120)
            ->select();

        $arrs = [
            'page'     => (int) $data['page'],
            'pageSize' => (int) $data['pageSize'],
            'rows'     => $rows->toArray(),
            'total'    => $total,
        ];

        return $arrs;
    }

    /**
     * [getPlatformInfo 详细]
     * @since  2025-06-04
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getPlatformInfo($data)
    {
        $info = self::seFilterBar($data)->cache(120)->find();
        $info->views = $info['views'] + 1;
        $info->save();

        return $info;
    }

    public function getTagsAttr($value)
    {
        return explode(',', $value);
    }

    public function getUpdateTimeAttr($value)
    {
        return date('m-d H:i', $value);
    }

     public function getPriceAttr($value)
    {
        if ($value) {
            return number_format($value, 2, '.', ',');
        }

        return '0.0';
        
    }
}